              package com.java.diagnostics.visualizer.displayer.html;
              
              import com.java.diagnostics.visualizer.coredisplayers.util.Messages;
              import com.java.diagnostics.visualizer.data.AggregateData;
              import com.java.diagnostics.visualizer.data.DataSet;
              import com.java.diagnostics.visualizer.data.UnstructuredData;
              import com.java.diagnostics.visualizer.data.Variant;
              import com.java.diagnostics.visualizer.data.ids.ID;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
              import java.io.BufferedWriter;
              import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
              import java.io.IOException;
              import java.io.InputStream;
              import java.util.logging.Level;
              import java.util.logging.Logger;

              import org.eclipse.core.runtime.FileLocator;
              import org.eclipse.core.runtime.IPath;
              import org.eclipse.core.runtime.Path;
              import org.eclipse.core.runtime.Platform;
              import org.eclipse.swt.graphics.ImageData;
              import org.eclipse.swt.graphics.ImageLoader;
              import org.osgi.framework.Bundle;
              
              public class UnstructuredDataWriter extends DataWriter
              {
                private static final String COMMENT = Messages.getString("UnstructuredDataWriter.alt.comment");
              
                private static final String GOODTHING = Messages.getString("UnstructuredDataWriter.alt.good");
              
                private static final String WARNING = Messages.getString("UnstructuredDataWriter.alt.warning");
              
                private static final String PROBLEM = Messages.getString("UnstructuredDataWriter.alt.problem");
                private static final String ICON_PATH = "icons/full/";
                private static final String ECLIPSE_PROBLEM_GIF = "obj16/error_tsk.gif";
                private static final String ECLIPSE_WARN_GIF = "obj16/warn_tsk.gif";
                private static final String GCMV_GOOD_GIF = "checkedout.gif";
                private static final String ECLIPSE_INFO_GIF = "obj16/info_tsk.gif";
                private static final String ORG_ECLIPSE_UI = "org.eclipse.ui";
                private static final String OUR_BUNDLE = "com.java.diagnostics.visualizer.coredisplayers";
                private static final String OPEN_PARAGRAPH_OPEN = "<p class=\"";
                private static final String OPEN_PARAGRAPH_CLOSE = "\">";
                private static final String CLOSE_PARAGRAPH = "</p>";
                private static final String PROBLEM_IMAGE = "problem.gif";
                private static final String WARNING_IMAGE = "warning.gif";
                private static final String GOODTHING_IMAGE = "good.gif";
                private static final String COMMENT_IMAGE = "info.gif";
              
                public UnstructuredDataWriter(DataSet dataSet, AggregateData representativeData, BufferedWriter buffer, ID id)
                {
                  super(dataSet, id, representativeData, buffer);
                }
              
                public static void initFiles(File directory)
                {
                 // Bundle bundle = Platform.getBundle("org.eclipse.ui");
                 // Bundle ourBundle = Platform.getBundle("com.java.diagnostics.visualizer.coredisplayers");
                  
	          copyImage(directory,"problem.gif");
                  copyImage(directory,"warning.gif");
                  copyImage(directory,"good.gif");
                  copyImage(directory, "info.gif");
                }
              
                private static void copyImage(File directory, String fileName)
                {
	         // deleted by wenguang.huang 2014年3月2日 下午9:50:49
                 // if (bundle != null)
                 //   try {
                 //     IPath path = new Path("icons/full/" + eclipseName);
                 //     InputStream stream = 
                 //       FileLocator.openStream(bundle, path, false);
                 //     ImageData data = new ImageData(stream);
                 //     ImageLoader imageLoader = new ImageLoader();
                 //     imageLoader.data = new ImageData[] { data };
              
                 //     File file = new File(directory, localName);
                 //     String saveName = file.getAbsolutePath();
                 //     imageLoader.save(saveName, 2);
                 //     stream.close();
                 //   } catch (IOException e) {
                 //     TRACE.log(Level.WARNING, e.toString(), e);
                 //   }
	         //added by wenguang.huang 2014年3月2日 下午9:51:33
	         String targetfile = directory.getAbsolutePath()+"\\"+fileName;
	         
             try {    
                  BufferedInputStream bis = new BufferedInputStream(UnstructuredDataWriter.class.getResourceAsStream(fileName));        
                  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(targetfile)));  
             try {  
                  int val = -1;  
                  while((val=bis.read())!=-1){                  
                  bos.write(val);  
                 }  
             bos.flush();  
             bos.close();  
             bis.close(); 
             //return true;
             } catch (IOException e) {  
             e.printStackTrace();              
             }  
           } catch (FileNotFoundException e) {  
             e.printStackTrace();  
          } 
  

			 //added end by wenguang.huang 2014年3月2日 下午9:51:33


                }
              
                public void writeData() throws IOException
                {
                  this.usingTable = false;
                  addDataHeading();
              
                  if (this.dataSet.getVariants().length > 1) {
                    this.usingTable = true;
                    openTable();
                  }
              
                  if (this.usingTable) {
                    writeColumnHeadings(
                      this.representativeData.getUnstructuredData(this.id).getDisplayName());
                  }
              
                  Variant[] variants = this.dataSet.getVariants();
                  for (int i = 0; i < variants.length; i++)
                  {
                    openRow();
                    Variant variant = variants[i];
              
                    if (this.usingTable) {
                      addTableHeader(variant.getLabel(), 1);
                    }
                    UnstructuredData data = variant.getUnstructuredData(this.id);
                    writeCell(data);
                    closeRow();
                  }
              
                  closeTable();
                }
              
                protected void addRow(String label, UnstructuredData data)
                  throws IOException
                {
                  if (this.usingTable)
                  {
                    addTableHeader(label, 1);
                    writeCell(data);
                  } else {
                    writeOut(data);
                  }
                }
              
                private void writeCell(UnstructuredData data) throws IOException
                {
                  if (data != null) {
                    if (this.usingTable) {
                      openCell();
                      writeOut(data);
                      closeCell();
                    }
                    else {
                      writeOut(data);
                    }
                  }
                  else if (this.usingTable)
                    addTableCell("");
                }
              
                protected void writeOut(UnstructuredData data)
                  throws IOException
                {
                  if (data != null) {
                    String[] problems = data.getProblems();
                    String[] warnings = data.getWarnings();
                    String[] goodthings = data.getGoodThings();
                    String[] comments = data.getComments();
              
                    appendContents(this.buffer, problems, PROBLEM, "problem.gif");
                    appendContents(this.buffer, warnings, WARNING, "warning.gif");
                    appendContents(this.buffer, goodthings, GOODTHING, "good.gif");
                    appendContents(this.buffer, comments, COMMENT, "info.gif");
                  }
                }
              
                private void appendContents(BufferedWriter buffer, String[] strings, String styleClass, String imageUrl)
                  throws IOException
                {
                  for (int i = 0; i < strings.length; i++) {
                    buffer.write("<p class=\"");
                    buffer.write(styleClass);
                    buffer.write("\">");
                    if (imageUrl != null) {
                      buffer.write("<img src=\"");
                      buffer.write(imageUrl);
                      buffer.write("\" alt=\"[");
                      buffer.write(styleClass);
                      buffer.write("]\">");
                    }
                    buffer.write(strings[i]);
                    buffer.write("</p>");
                  }
                }
              
                private void writeColumnHeadings(String label) throws IOException {
                  openRow();
                  addTableHeader(VARIANT, 1);
                  addTableHeader(label, 1);
                  closeRow();
                }
              }

